/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pnf_listConstract.java
 *
 * Created on Aug 20, 2010, 8:10:28 AM
 */

package GUI;

import config.User_function;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author RYUDO
 */
public class pnf_listContract extends javax.swing.JPanel {
    Vector tblData;
    Vector tblTitle;
    /** Creates new form pnf_listConstract */
    public pnf_listContract() {
        initComponents();
        fill_data(add_data());
        jPanelAdvancedSearch.setVisible(false);
    }

    private ResultSet add_data() {
        ResultSet data = User_function.get_tbl_Child_Nanny_all();
        return data;
    }
    
    private void fill_data(ResultSet data) {
        tblTitle = new Vector();
        tblTitle.add("Child ID");
        tblTitle.add("Nanny ID");
        tblTitle.add("Start Date");
        tblTitle.add("End Date");
        try {
            tblData = new Vector();
            while (data.next()) {
                Vector temp = new Vector();
                temp.add(data.getString(1));
                temp.add(data.getString(2));
                temp.add(data.getString(3));
                temp.add(data.getString(4));

                tblData.add(temp);
            }
            this.tbl_listContract.setModel(new DefaultTableModel(tblData, tblTitle));
        } catch (SQLException ex) {
            Logger.getLogger(pnf_listContract.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_listContract = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int vColIndex)
            {
                return false;
            }
        }
        ;
        jButtonSearch = new javax.swing.JButton();
        jCheckBoxAdvancedSearch = new javax.swing.JCheckBox();
        jPanelAdvancedSearch = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtStartDate = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        txtEndDate = new com.toedter.calendar.JDateChooser();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jComboBoxSearch = new javax.swing.JComboBox();
        jTextFieldSearch = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(227, 239, 255));

        tbl_listContract.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Child ID", "Nanny ID", "Start Date", "End Date"
            }
        ));
        tbl_listContract.setGridColor(new java.awt.Color(213, 213, 213));
        tbl_listContract.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tbl_listContractMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tbl_listContract);

        jButtonSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Search1.png"))); // NOI18N
        jButtonSearch.setText("Search");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jCheckBoxAdvancedSearch.setBackground(new java.awt.Color(227, 239, 255));
        jCheckBoxAdvancedSearch.setText("     Advanced Search");
        jCheckBoxAdvancedSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxAdvancedSearchActionPerformed(evt);
            }
        });

        jPanelAdvancedSearch.setBorder(javax.swing.BorderFactory.createTitledBorder("AdvanceSearch"));
        jPanelAdvancedSearch.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanelAdvancedSearch.setEnabled(false);
        jPanelAdvancedSearch.setFocusable(false);
        jPanelAdvancedSearch.setOpaque(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Start Date  :");

        txtStartDate.setDateFormatString("yyyy/MM/dd");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("End Date    :");

        txtEndDate.setDateFormatString("yyyy/MM/dd");

        javax.swing.GroupLayout jPanelAdvancedSearchLayout = new javax.swing.GroupLayout(jPanelAdvancedSearch);
        jPanelAdvancedSearch.setLayout(jPanelAdvancedSearchLayout);
        jPanelAdvancedSearchLayout.setHorizontalGroup(
            jPanelAdvancedSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAdvancedSearchLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(14, 14, 14)
                .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(jLabel3)
                .addGap(9, 9, 9)
                .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelAdvancedSearchLayout.setVerticalGroup(
            jPanelAdvancedSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAdvancedSearchLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanelAdvancedSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/edit_1.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        jComboBoxSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ChildID", "NannyID" }));

        jLabel1.setText("Search: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(63, 63, 63)
                .addComponent(jCheckBoxAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(191, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(212, 212, 212)
                .addComponent(jButtonSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                .addGap(80, 80, 80)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(78, 78, 78)
                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelAdvancedSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 404, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jCheckBoxAdvancedSearch)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(3, 3, 3)
                            .addComponent(jLabel1))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd)
                    .addComponent(jButtonSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tbl_listContractMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbl_listContractMouseReleased
        if(evt.getClickCount() == 2){
            int selection = this.tbl_listContract.getSelectedRow();
            Vector temp = new Vector();
            temp = ((Vector) tblData.get(selection));
            jdl_contractInfo ci = new jdl_contractInfo(null,true,temp,selection);
            ci.setVisible(true);

        }
}//GEN-LAST:event_tbl_listContractMouseReleased

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        // TODO add your handling code here:
        // invoke search function
        String searchString = jTextFieldSearch.getText();
        String searchType = (String) jComboBoxSearch.getSelectedItem();
        if (this.jCheckBoxAdvancedSearch.isSelected())
            fill_data(User_function.advanced_search_contract(searchString, searchType, txtStartDate, txtEndDate));
        else
            fill_data(User_function.search_contract(searchString, searchType));
}//GEN-LAST:event_jButtonSearchActionPerformed

    private void jCheckBoxAdvancedSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxAdvancedSearchActionPerformed
        // TODO add your handling code here:
        // select and modify search function
        boolean check = this.jCheckBoxAdvancedSearch.isSelected();
        if (check==true) {
            this.jPanelAdvancedSearch.setVisible(true);
        } else {
            this.jPanelAdvancedSearch.setVisible(false);
        }
}//GEN-LAST:event_jCheckBoxAdvancedSearchActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        // invoke add contract function and refresh table
        jdl_newContract nc = new jdl_newContract(null,true);
        nc.setVisible(true);
        fill_data(add_data());
}//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        // invoke edit contract function and refresh table
        int selection = this.tbl_listContract.getSelectedRow();
        if(selection != -1) {
            Vector temp_get_data = (Vector) tblData.get(selection);
            jdl_editContract ed = new jdl_editContract(null,true,temp_get_data.get(0).toString(),temp_get_data.get(1).toString());
            ed.setVisible(true);
            fill_data(add_data());
        } else {
            JOptionPane.showMessageDialog(this, "No contract selected");
        }
}//GEN-LAST:event_btnEditActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JCheckBox jCheckBoxAdvancedSearch;
    private javax.swing.JComboBox jComboBoxSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanelAdvancedSearch;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldSearch;
    private javax.swing.JTable tbl_listContract;
    private com.toedter.calendar.JDateChooser txtEndDate;
    private com.toedter.calendar.JDateChooser txtStartDate;
    // End of variables declaration//GEN-END:variables

}
